import { defineStore } from 'pinia'

export const useOrderStore = defineStore('order', {
  state: () => ({
    orderList: [],
    // 订单状态：0:待支付 1:待开启 2:履约中 3:已交付 4:待评价 5:已完成 6:已取消
    orderStatusType: [
      { status: 0, name: '待支付' },
      { status: 1, name: '待开启' },
      { status: 2, name: '履约中' },
      { status: 3, name: '已交付' },
      { status: 4, name: '待评价', tableShowNmae: "待评价" },
      { status: 5, name: '已完成' },
      { status: 6, name: '已取消' },
    ],
    payInfo: {}
  }),
  getters: {
    getOrderList: (state) => state.orderList,
    getOrderStatusType: (state) => state.orderStatusType,
    // 根据状态返回状态名称
    // 订单状态：0:待支付 1:待开启 2:履约中 3:已交付 4:待评价 5:已完成 6:已取消
    getOrderStatusName: (state) => (status: number, stepStatus: number, expireText: string) => {
      const orderStatusType = state.orderStatusType
      // 状态0（待支付）过期时显示已取消
      if (status === 0 && !stepStatus && expireText === '订单已过期') {
        return '已取消'
      }
      const orderStatus = orderStatusType.find(item => item.status === status)
      // 状态3（已交付）且stepStatus===6时显示待确认验收
      if (status === 3 && stepStatus === 6) {
        return '待确认验收'
      }
      return orderStatus ? orderStatus.name : '未知'
    }

  },
  actions: {
    setPayInfo(payInfo) {
      this.payInfo = payInfo
    }
  }
})